package com.bootdo.api;

import com.bootdo.backlist.converter.ConvertDomainUtils;
import com.bootdo.backlist.dto.MessageDTO;
import com.bootdo.backlist.form.MessageForm;
import com.bootdo.backlist.service.MessageService;
import com.bootdo.backlist.vo.MessageVO;
import com.bootdo.common.enums.BlackAuditEnum;
import com.bootdo.common.enums.ResultEnum;
import com.bootdo.common.interfaces.Add;
import com.bootdo.common.utils.*;
import jdk.nashorn.internal.objects.annotations.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 举报接口
 * @author Zengcx
 * @date 2019-04-11 21:29
 */
@RestController
@RequestMapping("/api/message")
@Slf4j
public class ApiMessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 举报
     * @param messageForm
     * @param bindingResult
     * @return
     */
    @PostMapping("/report")
    public R report(@Validated(Add.class)MessageForm messageForm , BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            log.error("【参数错误】messageForm={}",messageForm);
            return R.error(bindingResult.getFieldError().getDefaultMessage());
        }
        if(AccountValidatorUtil.isRegEX(messageForm.getBlackName())){
            return R.error("黑名单名称不能包含特殊字符");
        }
        if(AccountValidatorUtil.isRegEX(messageForm.getPlateNumber())){
            return R.error("车牌不能包含特殊字符");
        }
        if (!StringUtils.isBlank(messageForm.getPhone())){
            if (!AccountValidatorUtil.isMobile(messageForm.getPhone())){
                return R.error("请输入正确的手机号");
            }
        }
        if (messageForm.getPostImgArr().length > 0){
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < messageForm.getPostImgArr().length; i++) {
                sb.append(messageForm.getPostImgArr()[i] + ",");
            }
            StringBuilder imgStr = sb.deleteCharAt(messageForm.getPostImgArr().length - 1);
            messageForm.setPostImg(imgStr.toString());
        }
        return messageService.save(ConvertDomainUtils.convertObject(messageForm, MessageDTO.class));
    }

    /**
     * 举报列表
     * @param params
     * @return
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        if (null != params.get("isRisk")){
            params.put("sort","risk desc,rank asc,black_id desc,create_time desc");
        }else if (null != params.get("isRank")){
            params.put("sort","rank asc,black_id desc,create_time desc");
        }else if ( null != params.get("isRank") && null != params.get("isRisk")){
            params.put("sort","rank asc,risk desc,black_id desc,create_time desc");
        }else {
            params.put("sort","black_id desc,rank asc,risk desc,create_time desc");
        }
        if (params.get("offset") == null){
            params.put("offset",0);
        }
        if (params.get("limit") == null){
            params.put("limit",10);

        }

        Query query = new Query(params);
        List<MessageVO> messageList = ConvertDomainUtils.convertList(messageService.list(query),MessageVO.class);
        for (MessageVO messageVO : messageList) {
            messageVO.setTime(DateUtils.getTimeBeforeAccurate(messageVO.getCreateTime()).equalsIgnoreCase("前") ? "刚刚" : DateUtils.getTimeBeforeAccurate(messageVO.getCreateTime()));
            messageVO.setStatusStr(messageVO.getPostStatus() == BlackAuditEnum.审核中.getCode() ? "审核中" : messageVO.getPostStatus() == BlackAuditEnum.审核不通过.getCode() ? "审核不通过" : "审核通过" );
        }
        int total = messageService.count(query);
        PageUtils pageUtils = new PageUtils(messageList, total);
        return R.ok().put("data",pageUtils);
    }

    /**
     * 举报内容详情
     * @param blackId
     * @return
     */
    @GetMapping("/detail")
    public R detail(@RequestParam("blackId") Long blackId){
        if (blackId == null){
            return R.error(ResultEnum.PARAM_ERROR.getMessage());
        }

        MessageDTO messageDTO = messageService.get(blackId);
        if (messageDTO != null) {
            MessageVO messageVO = ConvertDomainUtils.convertObject(messageDTO, MessageVO.class);
            if (!StringUtils.isBlank(messageVO.getPostImg())) {
                String[] split = messageVO.getPostImg().split(",");
                messageVO.setPostImgArr(split);
            }
            return R.ok().put("data", messageVO);
        }
        return R.error(ResultEnum.DATA_NOT_EXIST.getMessage());
    }
}
